/* URBAN EXPLORER PROJECT
 * 
 * Author: Karthick Nanmaran (s1051713), School of Informatics, The University of Edinburgh
 * Email: karthicknanmaran@gmail.com
 * 
 * Supervisor: Dr.William Mackaness, 
 * 			   Institute of Geography,
 * 			   School of GeoSciences,
 * 			   The University of Edinburgh.
 * Email: william.mackaness@ed.ac.uk
 * 
 * This code can be found online at http://code.google.com/p/urban-explorers/
*/
package Firstdroid.Gps;
//import MainActivity;
import java.io.BufferedReader;
import java.lang.Object;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
//import com.google.android.maps.GeoPoint;
//import com.google.android.maps.MapActivity;
//import com.google.android.maps.MapView;
//import org.apache.commons.lang.StringUtils;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
//import android.widget.TextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.location.GpsStatus.NmeaListener;

/*When I wrote this, only God and I understood What I was doing! Now, God only knows! */
public class UrbanExplorer extends Service {
	private Location loc;
	private Dialog dialog;
	//private TextView coordinates;
	String DATA;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
    public void onCreate()
    {       
            super.onCreate();
    }
    @Override
    public void onStart(Intent intent, int startId) 
    {
		LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		LocationListener LL = new MyLocationListener();
		LM.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, LL);
        LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, LL);
        LM.addNmeaListener(new NmeaListener() {
        public void onNmeaReceived(long timestamp, String nmea) 
        { 
        	     	DATA = nmea;
        }});
	}
	
	@Override
	public void onDestroy() {
		Toast.makeText(this, "Tracking Stopped", Toast.LENGTH_LONG).show();
		System.exit(0);     
		}
	/* Class My Location Listener */
    public class MyLocationListener implements LocationListener
    {
    	public void onLocationChanged(Location location)
    	{
    		
    		if (location != null && DATA != null) {
    		String params;
			try {
				//Log.d("data", DATA);
				params = URLEncoder.encode(DATA, "UTF-8");
				Log.d("executing", params);
				String url = "http://xweb.geos.ed.ac.uk/~s1051713/cgi_bin/project/Server1.php?DATA="+params;
				//String url = "http://62.30.167.145/xampp/Server1.php?DATA="+params;
				Log.d("executing", url);
				//url = url+params;
				HttpPost httppost = new HttpPost(url);
				HttpClient client = new DefaultHttpClient();
	    		try {
	                HttpResponse httpResponse = client.execute(httppost);
	                Log.d("Status", "Request Sent " + httpResponse.getStatusLine().getStatusCode());
	        		} 
	            catch (Exception e) {
	        		// TODO Auto-generated catch block
	        		e.printStackTrace();
	        		} 
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			
    		}
    		} 	//Log.d("url", url);   
    	}
    public void onProviderDisabled(String provider) 
    	{
        Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_LONG ).show();
		}
    public void onProviderEnabled(String provider)
        {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_LONG ).show();
        }
    public void onStatusChanged(String provider, int status, Bundle extras)
        {
        	
        }    
    }/* End of Class MyLocationListener */
}   /* End of Class UrbanExplorer */